//
//  MessageCircleDetailHeaderView.swift
//  OnceNectar
//
//  Created by 法正磊 on 2018/2/7.
//  Copyright © 2018年 com.inwhoop. All rights reserved.
//

import UIKit
import DynamicBlurView
import Alamofire
import PKHUD
import SwiftyJSON
import Networking

protocol MessageCircleDetailHeaderViewDelegate: NSObjectProtocol {
    func memberTap()
    func headerTap()
    func followAc(id:String,btn:UIButton) -> Void
}

class MessageCircleDetailHeaderView: UITableViewHeaderFooterView, DelayExecution {
    @IBOutlet weak var topView: UIImageView!
    @IBOutlet weak var followButton: UIButton!
    @IBOutlet weak var bgImageView: UIImageView!

    @IBOutlet weak var newDyView: BaseCircleView!
    @IBOutlet weak var topCountLable: UILabel!
    @IBOutlet weak var centerImage: UIImageView!
    @IBOutlet var topBottomImage: [UIImageView]!
    
    @IBOutlet weak var bottomTitleLable: UILabel!
    @IBOutlet weak var bottomTitleHeight: NSLayoutConstraint!
    
    @IBOutlet weak var topViewHeight: NSLayoutConstraint!
    weak var delegate: MessageCircleDetailHeaderViewDelegate?
    
    var data: CircleDetailModel?
    
    
    /// 通过xib获取当前View
    ///
    /// - Returns: self
    func instanceMessageCircleDetailHeaderView() -> MessageCircleDetailHeaderView {
        let array = Bundle.main.loadNibNamed("\(MessageCircleDetailHeaderView.self)", owner: self, options: nil)
        let view = array?.first as! MessageCircleDetailHeaderView
        return view
    }
    
    func bindData(data: CircleDetailModel?){
        let type:String = UIDevice.current.model
        if type == "iPad" {
            bgImageView.isHidden = true
        }
        if let data1 = data {
            self.data = data1
            self.topCountLable.text = "圈子成员" + data1.circleUserCount! + "人"
            self.centerImage.sd_setImage(with: NSURL.init(string: "\(BaseURL)" + (data1.coverPath)!) as URL?, completed: nil)
            self.bgImageView.sd_setImage(with: NSURL.init(string: "\(BaseURL)" + (data1.coverPath)!) as URL?, completed: nil)
            self.bottomTitleLable.text = data1.circleRemark
            if (data1.circleUsers?.count)! < 5 {
                if let count = data1.circleUsers?.count{
                    let ucount = 5 - count
                    for index in 0 ..< ucount {
                        let image: UIImageView = self.topBottomImage[4 - index]
                        image.isHidden = true
                    }
                
                }
                }
            if var count = data1.circleUsers?.count{
                count = count > 5 ? 5 : count
                if count > 0 {
                    for index in 0 ..< count {
                        let image: UIImageView = self.topBottomImage[index]
                        let headModel: circleUserModel? = data1.circleUsers?[index]
                        let url: String? = "\(BaseURL)" + (headModel?.headIcon)!
                        image.sd_setImage(with: NSURL.init(string: url!) as URL?, completed: nil)
                    }
                }
                
            }
            if data1.beJoined == "true"  {
                self.followButton.isSelected = true
            }else {
                self.followButton.isSelected = false
            }
            if data1.circleRemark != nil {
                let remarkHeight:CGFloat = self.getTextHeigh(textStr: data1.circleRemark!, font: UIFont.systemFont(ofSize: 10.0), width: self.size.width - 50)
                bottomTitleHeight.constant += remarkHeight
                topViewHeight.constant += remarkHeight
//                self.snp.makeConstraints { (make) in
//                    make.width.equalToSuperview()
//                    make.height.equalTo(self.height + remarkHeight)
//                }
            }
        
        }
        setSelfHeight()
//        newDyView.snp.makeConstraints { (make) in
//            make.top.equalTo(topView.snp.bottom).offset(10)
//            make.bottom.equalTo(self.bottom).offset(-10)
//        }
    }
    
    private func setSelfHeight() {
        DispatchQueue.global().asyncAfter(deadline: DispatchTime.now() + 0.1) {
            DispatchQueue.main.async {

                let height = self.topViewHeight.constant + 42
                self.height = height
            }
        }
    }
    
    func getTextHeigh(textStr:String,font:UIFont,width:CGFloat) -> CGFloat {
        
        let normalText: NSString = textStr as NSString
        let size = CGSize.init(width: width, height: 1000)
        let dic = NSDictionary(object: font, forKey: NSAttributedStringKey.font as NSCopying)
        let stringSize = normalText.boundingRect(with: size, options: .usesLineFragmentOrigin, attributes: dic as? [NSAttributedStringKey : Any], context:nil).size
        return stringSize.height
    }
    
    override func awakeFromNib() {
        topView.layoutIfNeeded()
        followButton.layer.cornerRadius = followButton.height / 2
        followButton.layer.borderWidth = 1
        followButton.layer.borderColor = ONColor.pirceLabelColor.cgColor
        
    }
    
    @IBAction func memberTap(_ sender: UIButton) {
        delegate?.memberTap()
    }
    
    @IBAction func headerTap(_ sender: UITapGestureRecognizer) {
        delegate?.headerTap()
    }
    
    
    @IBAction func followTap(_ sender: UIButton) {
        self.delegate?.followAc(id: (self.data?.id)!, btn: sender)
    }
    
    @IBAction func tap4(_ sender: Any) {
        let messageCirclePersonalCenterVC = PersonInfoViewController(nibName: "\(PersonInfoViewController.self)", bundle: nil)
        messageCirclePersonalCenterVC.userId = (data?.circleUsers![3].id!)!
        if let dele: UIViewController = self.delegate as? UIViewController {
            dele.navigationController?.pushViewController(messageCirclePersonalCenterVC, animated: true)
        }
        
    }
    @IBAction func tap1(_ sender: Any) {
        let messageCirclePersonalCenterVC = PersonInfoViewController(nibName: "\(PersonInfoViewController.self)", bundle: nil)
        messageCirclePersonalCenterVC.userId = (data?.circleUsers![0].id!)!
        if let dele: UIViewController = self.delegate as? UIViewController {
            dele.navigationController?.pushViewController(messageCirclePersonalCenterVC, animated: true)
        }
        
    }
    @IBAction func tap3(_ sender: Any) {
        let messageCirclePersonalCenterVC = PersonInfoViewController(nibName: "\(PersonInfoViewController.self)", bundle: nil)
        messageCirclePersonalCenterVC.userId = (data?.circleUsers![2].id!)!
        if let dele: UIViewController = self.delegate as? UIViewController {
            dele.navigationController?.pushViewController(messageCirclePersonalCenterVC, animated: true)
        }
    }
    @IBAction func tap2(_ sender: Any) {
        let messageCirclePersonalCenterVC = PersonInfoViewController(nibName: "\(PersonInfoViewController.self)", bundle: nil)
        messageCirclePersonalCenterVC.userId = (data?.circleUsers![1].id!)!
        if let dele: UIViewController = self.delegate as? UIViewController {
            dele.navigationController?.pushViewController(messageCirclePersonalCenterVC, animated: true)
        }
    }
    @IBAction func tap5(_ sender: Any) {
        let messageCirclePersonalCenterVC = PersonInfoViewController(nibName: "\(PersonInfoViewController.self)", bundle: nil)
        messageCirclePersonalCenterVC.userId = (data?.circleUsers![4].id!)!
        if let dele: UIViewController = self.delegate as? UIViewController {
            dele.navigationController?.pushViewController(messageCirclePersonalCenterVC, animated: true)
        }    }
    
}
